package ro.mse.search.main;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

import ro.mse.search.analyzer.SnowballAnalyzer;

public class Searcher {

	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			throw new Exception("Usage: java " + Searcher.class.getName()
					+ " <index dir> <query>");
		}
		String indexDir = args[0]; // 1
		String q = "judec~0.4"; // 2
		search(indexDir, q);
	}

	public static void search(String indexDir, String q) throws Exception {
		IndexSearcher is = new IndexSearcher(indexDir); // 3
		QueryParser parser = new QueryParser("contents",
				new SnowballAnalyzer("RoAnalyzer")); // 4
		Query query = parser.parse(q); // 4
		long start = System.currentTimeMillis();
		TopDocs hits = is.search(query, 10); // 5
		long end = System.currentTimeMillis();
		System.err.println("Found " + hits.totalHits
				+ // 6
				" document(s) (in " + (end - start)
				+ " milliseconds) that matched query '" + q + "':");
		for (int i = 0; i < hits.scoreDocs.length; i++) {
			ScoreDoc scoreDoc = hits.scoreDocs[i];
			Document doc = is.doc(scoreDoc.doc); // 7
			System.out.println(doc.get("filename")+" "+scoreDoc.score); // 8
		}
		is.close(); // 9
	}
}
